#include "stdafx.h"
#include "IBasicRenderObjectScrollBar.h"
#include "IBasicResourceMgr.h"
#include "BasicSkinCanvas.h"

namespace BasicSkinView
{
	IBasicRenderObjectScrollBar::IBasicRenderObjectScrollBar( bool bVert )
		:m_pBkImage(NULL)
		,m_bFillBkgnd(false)
	{
		m_bVert = bVert;
		memset(&m_si,0,sizeof(m_si));

		if (bVert)
			m_pBkImage = IBasicResourceMgr::ILoadResImage(L"PublicControls\\ScrollbarButton.png");
	}

	IBasicRenderObjectScrollBar::~IBasicRenderObjectScrollBar()
	{
		if (m_pBkImage)
			delete m_pBkImage;
		m_pBkImage = NULL;
	}

	void IBasicRenderObjectScrollBar::RenderObject( CBasicSkinCanvas* pCanvas )
	{
		if (!m_pBkImage)
			return;
		RECT rcTh;
		GetThumbRect(&rcTh);
		if (m_bFillBkgnd)
			pCanvas->FillRectangle(&this->m_rcRender,254,220,224,227);
		pCanvas->DrawTitleButton(m_pBkImage,rcTh.left,rcTh.top,10,rcTh.bottom-rcTh.top,1,2,true);
	}

	int IBasicRenderObjectScrollBar::ISetScrollInfo( LPSCROLLINFO psi )
	{
		m_si = *psi;
		if (m_si.nPos < 0)
			m_si.nPos = 0;
		if (m_si.nPos > m_si.nMax-m_si.nPage+1)
			m_si.nPos = m_si.nMax-m_si.nPage;
		return true;
	}

	bool IBasicRenderObjectScrollBar::IsScrollBarCanShow()
	{
		return (m_si.nMax - m_si.nMin+1 > m_si.nPage);
	}

	void IBasicRenderObjectScrollBar::GetThumbRect( LPRECT lpRect )
	{
		int nH = m_rcRender.bottom-m_rcRender.top;
		int nThumbSize = 0;
		DWORD dwRange = ((DWORD)(m_si.nMax-m_si.nMin)) + 1;

		int cpx = 16;
		int cpxThumb = MulDiv(nH,m_si.nPage,dwRange);
		cpxThumb = max(cpxThumb,cpx);
		int denom = dwRange - (m_si.nPage? m_si.nPage : 1);
		int dx = nH-cpxThumb;
		int yStart = MulDiv(m_si.nPos,dx, denom)  + m_rcRender.top;
		::SetRect(lpRect,m_rcRender.left,yStart,m_rcRender.left+10,yStart+cpxThumb);
	}

	void IBasicRenderObjectScrollBar::ISetScrollInfoPos( int nPos )
	{
		m_si.nPos = nPos;
	}
}